<template>
  <upload-excel :onSuccess="onSuccess"></upload-excel>
</template>

<script setup>
import UploadExcel from '@/utils/UploadExcel.vue'
import { importList } from '@/http/user.js'
import { USER_RELATIONS, formatDate } from './utilts'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
/**
 * 数据解析成功之后的回调 成功回调需要筛选数据，将解析过后的数据，过滤成后端对应的数据结构
 */
const onSuccess = async ({ header, results }) => {
  const updateData = generateData(results)
  
  console.log(updateData,"回调成功的数据")

  await importList(updateData)

  ElMessage.success({
    message: results.length + '成功',
    type: 'success'
  })
  router.push('/user/manage')
}

/**
 * 筛选数据
 */
const generateData = results => {
  const arr = []
  results.forEach(item => {
    const userInfo = {}
    Object.keys(item).forEach(key => {
      if (USER_RELATIONS[key] === 'openTime') {
        userInfo[USER_RELATIONS[key]] = formatDate(item[key])
        return
      }
      userInfo[USER_RELATIONS[key]] = item[key]
    })
    arr.push(userInfo)
  })
  return arr
}

</script>

<style lang="scss" scoped></style>
